<?php
/**
 * RadioEveningController.class.php
 * 晚间电台控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-04-27
 */

namespace Admin\Controller;

class RadioEveningController extends BaseController {

	/**
	 * 电台列表获取
	 * @param 	$p
	 * @return 	json 	$list
	 */
	function getList(){
		$condition 	= $this->request('condition', []);
		$page 	= $this->request('p', 1);
		$pageHtml 	= true;

		$this->response([
			'list'	=> D('RadioEvening')->getList($condition, $page, $pageHtml),
			'page_html'	=> $pageHtml
		]);
	}

	/**
	 * 添加电台
	 * @param 	$title
	 * @param 	$banner
	 * @param 	$audio
	 * @return 	boolean
	 */
	function addItem(){
		$client 	= $this->unEmptyParam(['title', 'title_ii', 'banner', 'audio', 'audio_ii']);

		$title 	= $client['title'];
		$titleII 	= $client['title_ii'];
		$banner 	= $client['banner'];
		$audio 	= $client['audio'];
		$audioII 	= $client['audio_ii'];
		$onlineTime 	= $this->request('online_date') ? strtotime($this->request('online_date')) : time();
		$status 	= $this->request('online_date') ? 2 : 1;
		$content 	= $this->request('content');

		$banner 	= strchr($banner, '/server');
		$audio 	= strchr($audio, '/server');
		$audioTime 	= get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');
		$audioII 	= strchr($audioII, '/server');
		$audioIItime 	= get_audio_info(strchr($audioII, 'Uploads'), 'playtime_seconds');

		$radioId 	= D('RadioEvening')->add([
			'title'	=> $title,
			'title_ii'	=> $titleII,
			'banner'	=> $banner,
			'audio'	=> $audio,
			'audio_time'	=> $audioTime,
			'audio_ii'	=> $audioII,
			'audio_ii_time'	=> $audioIItime,
			'content'	=> $content,
			'create_time'	=> time(),
			'online_time'	=> $onlineTime,
			'status'	=> $status
		]);

		$this->response();
	}

	/**
	 * 编辑电台
	 * @param 	$radio_id
	 * @param 	$radio_info
	 * @return 	boolean
	 */
	function updateItem(){
		$client 	= $this->unEmptyParam(['radio_id', 'radio_info']);

		$radioId 	= $client['radio_id'];
		$radioInfo 	= $client['radio_info'];

		//字段整理
		if($radioInfo['audio']){
			$radioInfo['audio']	= strchr($radioInfo['audio'], '/server');
			$radioInfo['audio_time']	= get_audio_info(strchr($radioInfo['audio'], 'Uploads'), 'playtime_seconds');
		}
		if($radioInfo['audio_ii']){
			$radioInfo['audio_ii']	= strchr($radioInfo['audio_ii'], '/server');
			$radioInfo['audio_ii_time']	= get_audio_info(strchr($radioInfo['audio_ii'], 'Uploads'), 'playtime_seconds');
		}
		if($radioInfo['banner']){
			$radioInfo['banner']	= strchr($radioInfo['banner'], '/server');
		}
		$radioInfo['update_time']	= time();
		$result 	= D('RadioEvening')->where('id = %d', $radioId)->save($radioInfo);
		
		$this->response();
	}
	
}